<?php
    class Templates{
        private $_vars = array();//save common var
        private $_langs = array();//host var
        private $tpl_file;//templates_file_url;
        private $parse_file;//templates_compile_url
        private $cache_file;//cache_file_url
        
        public function __construct(){
            $this->is_dir_exists();
            $this->getLang();
        }
   
    public function display($file){
        if($file[0]=='/'){
            $this->tpl_file =   APP_DIR.'/view'.$file;
            $files  =   explode("/",$this->tpl_file);
            $file   =   $files[count($files)-1];
        }else{
            $this->tpl_file = TPL_DIR.$file;//set want load templates url
        }
        if(!file_exists($this->tpl_file)){
                exit($this->tpl_file."the file is not fond");
        }
        $this->parse_file = TPL_C_DIR.md5($file).'.'.$file.'.php';//the thempate_compile url;
        include LIB_AIR.'/Driver/Parse/Parse.class.php';//load thempate-analyze class
        $parse  =   new Parse($this->tpl_file);//initalizing template-analyze class
        $parse->compile($this->parse_file,$this->tpl_file);//analyze thempate-file,create compile file
        //is need create cache?
        $this->cache();
    }
    public function assign($var,$value){
        if(isset($var)&&!empty($var)){//is the var haven't set, and value isn't null
                $this->_vars[$var]=$value;//$save the value in the _vars
        }else{
            exit('assign is error');
        }
    }
    private function getLang(){
            if(file_exists(APP_LANG.'/common.xml')){
                $sxe    =   simplexml_load_file(APP_LANG.'/common.xml');
                $taglib =   $sxe->xpath('/root/taglib');//user xpath way read the thing
                foreach($taglib as $value){
                        $this->_langs["$value->name"] =   $value->value;
                }
            }
    }
    private function is_dir_exists(){
            if(!is_dir(TPL_DIR)){//catalog if the template file is fond
                exit(TPL_DIR." the template file is not find");
            }
            if(!is_dir(TPL_C_DIR)){
                exit("the template_compile file is notfind");
            }
            if(!is_dir(CACHE_DIR)){
                exit("the cache file is not find");
            }
    }

    private function cache(){
        $this->cache_file=CACHE_DIR.md5($file).'.'.$file.'.html';//设置缓存文件路径
        //如果开启缓存,缓存文件存在且模板文件没有被修改过,直接载入缓存文件
        if(IS_CACHE){
        if(file_exists($this->cache_file)&&filemtime($this->cache_file)>=filemtime($this->parse_file)){
        include $this->cache_file;//载入缓存文件
        return ;
        }
        }
        //判断是否开启缓存,如果开启就生成静态html文件,否则,直接载入编译文件
        IS_CACHE? ob_start():null;
        if(IS_CACHE){
        include $this->parse_file;
        file_put_contents($this->cache_file,ob_get_contents());//生成静态html缓存文件
        ob_end_clean();
        include $this->cache_file;//载入静态html缓存文件
        }else{
        include $this->parse_file;//载入编译文件
        }
    }

}
?>